package com.zjdiepu.www.biz.yw;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.model.yw.ext.YwCarinfoEditHisExt;
import com.zjdiepu.www.dao.yw.YwCarinfoEditHisMapper;
import com.zjdiepu.www.model.yw.dto.YwCarinfoEditHis;
import com.zjdiepu.www.ibiz.yw.IYwCarinfoEditHisService;

/**
 * yw_车辆信息历史表Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2018年01月29日 15时59分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwCarinfoEditHisServiceImpl extends BaseService implements IYwCarinfoEditHisService {
	public static final Logger logger = LoggerFactory.getLogger(YwCarinfoEditHisServiceImpl.class);

	@Autowired
	private YwCarinfoEditHisMapper ywCarinfoEditHisMapper;
	
	@Override
	public void saveUpdateAndDelete(YwCarinfoEditHis ywCarinfoEditHis, OperateType operType) {
		switch (operType) {
		case add:
			ywCarinfoEditHis.initInsertData(ywCarinfoEditHis.getModifier(), ywCarinfoEditHis.getModifierName());
			ywCarinfoEditHisMapper.insert(ywCarinfoEditHis);
			break;
		case edit:
			ywCarinfoEditHis.initUpdateData(ywCarinfoEditHis.getModifier(), ywCarinfoEditHis.getModifierName());
			// 影响的记录数
			int updateCount = ywCarinfoEditHisMapper.updateByPrimaryKeySelective(ywCarinfoEditHis);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywCarinfoEditHis.initDeleteData(ywCarinfoEditHis.getModifier(), ywCarinfoEditHis.getModifierName());
			// 影响的记录数
			int deleteCount = ywCarinfoEditHisMapper.updateByPrimaryKeySelective(ywCarinfoEditHis);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwCarinfoEditHis> selectByExt(YwCarinfoEditHisExt ywCarinfoEditHisExt) {
		return ywCarinfoEditHisMapper.selectByExt(ywCarinfoEditHisExt);
	}
	
	@Override
	public int countByExt(YwCarinfoEditHisExt ywCarinfoEditHisExt){
		return ywCarinfoEditHisMapper.countByExt(ywCarinfoEditHisExt);
	}
	
	@Override
	public YwCarinfoEditHis selectById(String id) {
		return ywCarinfoEditHisMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywCarinfoEditHisMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwCarinfoEditHis ywCarinfoEditHis) {
		return ywCarinfoEditHisMapper.countRefined(ywCarinfoEditHis);
	}

	@Override
	public YwCarinfoEditHis selectRefined(YwCarinfoEditHis ywCarinfoEditHis) {
		return ywCarinfoEditHisMapper.selectRefined(ywCarinfoEditHis);
	}
	
	@Override
	public int deletedRefined(YwCarinfoEditHis ywCarinfoEditHis) {
		return ywCarinfoEditHisMapper.deletedRefined(ywCarinfoEditHis);
	}

	@Override
	public boolean repeat(YwCarinfoEditHis ywCarinfoEditHis) {
		int count = countRefined(ywCarinfoEditHis);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywCarinfoEditHis.getId())) {
			return true;
		}
		YwCarinfoEditHis dbItem = selectRefined(ywCarinfoEditHis);
		if(null == dbItem) {
			return false;
		}
		return !ywCarinfoEditHis.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywCarinfoEditHisMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<YwCarinfoEditHis> ywCarinfoEditHiss) {
		return ywCarinfoEditHisMapper.insertBatch(ywCarinfoEditHiss);
	}
}
